import 'package:flutter/material.dart';
import '../utils/log_utils.dart';
class NavigatorMonitor extends NavigatorObserver{
  @override
  void didPop(Route route, Route previousRoute) {
    super.didPop(route, previousRoute);
    NavigatorMonitorLog.saveLog(LogType.Pop, previousRoute, route);
  }
  @override
  void didRemove(Route route, Route previousRoute) {
    super.didRemove(route, previousRoute);
    NavigatorMonitorLog.saveLog(LogType.Remove, route, previousRoute);
  }
  @override
  void didPush(Route route, Route previousRoute) {
    super.didPush(route, previousRoute);
    NavigatorMonitorLog.saveLog(LogType.Push, route, previousRoute);
  }
  @override
  void didReplace({Route newRoute, Route oldRoute}) {
    super.didReplace(newRoute:newRoute, oldRoute:oldRoute);
    NavigatorMonitorLog.saveLog(LogType.Replace, newRoute, oldRoute);
  }
  @override
  void didStartUserGesture(Route route, Route previousRoute) {
    super.didStartUserGesture(route, previousRoute);
    NavigatorMonitorLog.saveLog(LogType.StartUserGesture, route, previousRoute);
  }
  @override
  void didStopUserGesture() {
    super.didStopUserGesture();
  }
  @override
  noSuchMethod(Invocation invocation) {
    return super.noSuchMethod(invocation);
  }
}

class NavigatorMonitorLog{
  static void saveLog(LogType type,Route route, Route previousRoute) {
    String fwhere="", twhere="";
    if (route != null && route.settings!=null&& route.settings.name!=null&& route.settings.name.isNotEmpty) {
      twhere = route.settings.name;
    }
    if (previousRoute != null && previousRoute.settings!=null&& previousRoute.settings.name!=null&& previousRoute.settings.name.isNotEmpty) {
      fwhere = previousRoute.settings.name;
    }
    if (fwhere.isNotEmpty||twhere.isNotEmpty) {
      LogUtils.instance.log(Log(
        what: type,
        when: DateTime.now(),
        fWhere: fwhere,
        tWhere: twhere,
        ext: ""
      ));
    }
  }
}